package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import Database.AddDbFunctions;
import Database.GetDbFunctions;
import Database.Recs.CategoryRec;
import Logging.Log;

public class AddItem extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2585183537939113688L;
	private JButton buttonSave;
	private JButton buttonCancel;
	private JTextField textAccountNumber;
	private JTextField textBankNumber;
	private JTextField textAccountDescription;
	private JTextField textCategoryName;
	private JTextField textCategoryDescription;
	
	private JTree treeParent;
	private int dim; 		//0 is Category, 2 is Account
	private AddDims aDims;
	private JLabel labelAccountNumber;
	private JLabel labelAccountDescription;
	private JLabel labelBankNumber;
	private JLabel labelParent;
	private JLabel labelCategoryDescription;
	private JLabel labelCategoryName;
	private JScrollPane scrollPanelForTree;
	
	private DefaultTreeModel dtm = new DefaultTreeModel(null);
	
	public AddItem(AddDims adims) 
	{
		initData();
		initComponents();
		aDims = adims;
	}
	
	private void initComponents() {
		this.setTitle("Add dimension");
		this.setIconImage(new ImageIcon("coins-icon.png").getImage());
		setLayout(null);
		add(getLabelAccountNumber());
		add(getLabelAccountDescription());
		add(getLabelBankNumber());
		add(getTextAccountNumber());
		add(getTextBankNumber());
		add(getTextAccountDescription());
		add(getTextCategoryName());
		add(getTextCategoryDescription());
		add(getScrollPanelForTree());
		add(getLabelParent());
		add(getLabelCategoryDescription());
		add(getLabelCategoryName());
		add(getButtonSave());
		add(getButtonCancel());
		setSize(616, 345);
	}
	
	private void initData()
	{
		Hashtable<String, CategoryRec> categories = GetDbFunctions.getCategoryHash();
		
		DefaultMutableTreeNode root;
		
		root = new DefaultMutableTreeNode("Root");
		
		loadTreeNodes(root, categories,"",1);
		
		dtm = new DefaultTreeModel(root);
	}
	
	private void loadTreeNodes(DefaultMutableTreeNode parent, Hashtable<String, CategoryRec> nodes, String structBuilder, int position)
	{
		CategoryRec node;
		String key;
		
		do
		{
			key = structBuilder + (structBuilder.isEmpty() ? "" : ".") + (position < 10 ? "0"+position : position);
			node = nodes.get(key);
			
			if(node != null)
			{
				DefaultMutableTreeNode child = new DefaultMutableTreeNode(node.getName());
				loadTreeNodes(child, nodes, key, 1);
				parent.add(child);
				position++;
			}
		}while(node!=null);
	}
	
	
	private JButton getButtonSave() {
		if (buttonSave == null) {
			buttonSave = new JButton();
			buttonSave.setText("Save");
			buttonSave.setBounds(86, 137, 81, 26);
			buttonSave.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					onButtonSave(event);
				}
			});
		}
		return buttonSave;
	}
	private JButton getButtonCancel() {
		if (buttonCancel == null) {
			buttonCancel = new JButton();
			buttonCancel.setText("Close");
			buttonCancel.setBounds(199, 136, 81, 26);
			buttonCancel.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					onButtonCancel(event);
				}
			});
		}
		return buttonCancel;
	}
	private JTextField getTextAccountNumber() {
		if (textAccountNumber == null) {
			textAccountNumber = new JTextField();
			textAccountNumber.setBounds(135, 4, 148, 20);
		}
		return textAccountNumber;
	}
	private JTextField getTextBankNumber() {
		if (textBankNumber == null) {
			textBankNumber = new JTextField();
			textBankNumber.setBounds(136, 31, 146, 20);
		}
		return textBankNumber;
	}
	private JTextField getTextAccountDescription() {
		if (textAccountDescription == null) {
			textAccountDescription = new JTextField();
			textAccountDescription.setBounds(137, 58, 146, 62);
		}
		return textAccountDescription;
	}
	
	private JTextField getTextCategoryDescription() {
		if (textCategoryDescription == null) {
			textCategoryDescription = new JTextField();
			textCategoryDescription.setBounds(420, 40, 63, 20);
		}
		return textCategoryDescription;
	}
	private JTextField getTextCategoryName() {
		if (textCategoryName == null) {
			textCategoryName = new JTextField();
			textCategoryName.setBounds(420, 11, 63, 20);
		}
		return textCategoryName;
	}
	
	
	
	private JLabel getLabelCategoryName() {
		if (labelCategoryName == null) {
			labelCategoryName = new JLabel();
			labelCategoryName.setText("Category name");
			labelCategoryName.setBounds(312, 12, 100, 15);
		}
		return labelCategoryName;
	}
	private JLabel getLabelCategoryDescription() {
		if (labelCategoryDescription == null) {
			labelCategoryDescription = new JLabel();
			labelCategoryDescription.setText("Description");
			labelCategoryDescription.setBounds(311, 39, 100, 15);
		}
		return labelCategoryDescription;
	}
	private JLabel getLabelParent() {
		if (labelParent == null) {
			labelParent = new JLabel();
			labelParent.setText("Category parent");
			labelParent.setBounds(314, 80, 106, 15);
		}
		return labelParent;
	}
	private JLabel getLabelBankNumber() {
		if (labelBankNumber == null) {
			labelBankNumber = new JLabel();
			labelBankNumber.setText("Bank code");
			labelBankNumber.setBounds(27, 32, 95, 15);
		}
		return labelBankNumber;
	}
	private JLabel getLabelAccountDescription() {
		if (labelAccountDescription == null) {
			labelAccountDescription = new JLabel();
			labelAccountDescription.setText("Description");
			labelAccountDescription.setBounds(25, 59, 92, 15);
		}
		return labelAccountDescription;
	}
	private JLabel getLabelAccountNumber() {
		if (labelAccountNumber == null) {
			labelAccountNumber = new JLabel();
			labelAccountNumber.setText("Account number");
			labelAccountNumber.setBounds(25, 7, 106, 15);
		}
		return labelAccountNumber;
	}
	
	private JTree getTreeParent() {
		if (treeParent == null) {
			treeParent = new JTree();
			treeParent.setModel(dtm);
			treeParent.setDoubleBuffered(false);
			treeParent.setBorder(null);
			treeParent.setSelectionRow(0);
		}
		return treeParent;
	}
	
	private JScrollPane getScrollPanelForTree() {
		if (scrollPanelForTree == null) {
			scrollPanelForTree = new JScrollPane();
			scrollPanelForTree.setBounds(422, 74, 150, 200);
			scrollPanelForTree.setViewportView(getTreeParent());
		}
		return scrollPanelForTree;
	}
	
	
	// ActionListeners
	private void onButtonSave(ActionEvent event) {
		
		switch (dim) {
		case 0:
				AddDbFunctions.addCategory(textCategoryName.getText(), textCategoryDescription.getText(), treeParent.getSelectionPath()!=null ? treeParent.getSelectionPath().toString() : null);
				initData();
				treeParent.setModel(dtm);
				treeParent.setSelectionRow(0);
				scrollPanelForTree.repaint();
				break;
				
		case 2: 
				Integer bankNumber = null;
				if(textBankNumber.getText()!=null && !textBankNumber.getText().isEmpty())
					try
					{
						bankNumber = new Integer(textBankNumber.getText());
					}
					catch (Exception e)
					{
						Log.log("Zly format cisla");
						break;
					}
				AddDbFunctions.addAccount(textAccountNumber.getText(), bankNumber, textAccountDescription.getText());
				break;

		default:
			break;
		}
		
		textAccountNumber.setText("");
		textBankNumber.setText("");
		textAccountDescription.setText("");
		textCategoryName.setText("");
		textCategoryDescription.setText("");
		
	}
	private void onButtonCancel(ActionEvent event) {
		this.setVisible(false);
		aDims.initData();
	}
	
	//Getters and Setters
	public int getDim() {
		return dim;
	}
	public void setDim(int dim) {
		this.dim = dim;
		switch (dim)
		{
			case 0:	
					textAccountNumber.setVisible(false);
					textBankNumber.setVisible(false);
					textAccountDescription.setVisible(false);
					textCategoryName.setVisible(true);
					textCategoryDescription.setVisible(true);
					scrollPanelForTree.setVisible(true);
					labelAccountNumber.setVisible(false);
					labelBankNumber.setVisible(false);
					labelAccountDescription.setVisible(false);
					labelCategoryName.setVisible(true);
					labelCategoryDescription.setVisible(true);
					labelParent.setVisible(true);
					
					break;
			case 2:	
				textAccountNumber.setVisible(true);
				textBankNumber.setVisible(true);
				textAccountDescription.setVisible(true);
				textCategoryName.setVisible(false);
				textCategoryDescription.setVisible(false);
				scrollPanelForTree.setVisible(false);
				labelAccountNumber.setVisible(true);
				labelBankNumber.setVisible(true);
				labelAccountDescription.setVisible(true);
				labelCategoryName.setVisible(false);
				labelCategoryDescription.setVisible(false);
				labelParent.setVisible(false);
				
				break;
		}
	}
}
